<script setup>
import MyNav from "@/components/MyNav.vue";
import MyHead from "@/components/MyHead.vue";
import MyTable from "@/components/MyTable.vue";
import {onMounted, ref, shallowReactive, shallowRef} from "vue";
import {dateFormat, myRemove, myPage, myRemoveBatch} from "@/util";
import {pageApi, pageLikeTitleApi, removeApi, removeBatchApi} from "@/api/SchoolApi";
import MyPager from "@/components/MyPager.vue";
import MyList from "@/components/MyList.vue";
import {excelApi} from "@/api/axios";
import router from "@/router";

/* ==================== 分页查询 ==================== */
// 表格数据 + 分页数据 + 学校名称
let records = ref();
let pageInfo = shallowReactive({'pageNum': 1, 'pageSize': 5, 'fn': page});
let schoolTitle = shallowRef();

async function page(pageNum = pageInfo['pageNum'],
                    pageSize = pageInfo['pageSize']) {
  let config = {
    'api': pageApi,
    'params': {'pageNum': pageNum, 'pageSize': pageSize},
    'records': records,
    'pageInfo': pageInfo
  }

  // 若学校名不为空，则使用学校名作为分页条件
  if (schoolTitle.value) {
    config['params']['title'] = schoolTitle.value;
    config['api'] = pageLikeTitleApi;
  }

  // 发送分页请求
  await myPage(config);
}

/* ==================== 单条添加 ==================== */
function insert() {
  router.push('/rms/SchoolInsert');
}

/* ==================== 批量删除 ==================== */
// 批量删除主键数组
let ids = [];

function selectionChange(rows) {
  ids = rows.map(e => e['id']);
}

function removeBatch() {
  myRemoveBatch({'ids': ids, 'api': removeBatchApi, 'fn': page})
}

/* ==================== 打印报表 ==================== */
function excel() {
  excelApi('/school/excel', '学校统计表');
}

/* ==================== 单条删除 ==================== */
function remove(row) {
  myRemove({'id': row['id'], 'api': removeApi, 'fn': page});
}

/* ==================== 单条修改 ==================== */
function update(row) {
  sessionStorage.setItem('school', JSON.stringify(row));
  router.push('/rms/SchoolUpdate');
}

/* ==================== 搜索学校名 ==================== */
function pageByTitle(val) {
  // 仅当输入框有值，或者当前处于按条件分页状态时，发送分页请求
  if (val || schoolTitle.value) {
    schoolTitle.value = val;
    page();
  }
}

/* ==================== 查看详情 ==================== */
// 学校详情抽屉 + 学校详情列表数据
let detailDrawer = shallowRef();
let detailItems = ref();

function detail(row) {
  detailItems.value = [
    {'label': '学校名称', 'value': row['title']},
    {'label': '学院名称', 'value': row['college']},
    {'label': '专业名称', 'value': row['major']},
    {'label': '班级名称', 'value': row['clazz']},
    {'label': '学校地址', 'value': row['address'], 'type': 'card'},
    {'label': '创建时间', 'value': row['created'], 'format': dateFormat},
    {'label': '修改时间', 'value': row['updated'], 'format': dateFormat},
    {'label': '学校描述', 'value': row['info'], 'type': 'card'},
  ];
  detailDrawer.value = true;
}

/* ==================== 加载函数 ==================== */
onMounted(() => page());

</script>

<template>

  <my-nav :items="[
    {'icon': 'User', 'label': '资源管理'},
    {'icon': 'User', 'label': '学校列表'},
  ]"/>

  <my-head v-if="records" :items="[
    {'type': 'ipt', 'span': 5, 'placeholder': '学校名称', 'fn': pageByTitle},
    {'type': 'insertBtn', 'offset': 16, 'fn': insert},
    {'type': 'deleteBtn', 'fn': removeBatch},
    {'type': 'excelBtn', 'fn': excel},
  ]"/>

  <my-table v-if="records" :records="records" :columns="[
    {'column': '学校名称', 'property': 'title'},
    {'column': '学院名称', 'property': 'college'},
    {'column': '专业名称', 'property': 'major'},
    {'column': '班级名称', 'property': 'clazz'},
    {'column': '学校地址', 'property': 'address', 'type': 'card'},
    {'column': '学校描述', 'property': 'info', 'type': 'card'},
  ]" :buttons="[
    {'type': 'info', 'fn': detail},
    {'type': 'update', 'fn': update},
    {'type': 'delete', 'fn': remove},
  ]" :selection-change="selectionChange"/>

  <my-pager :page-info="pageInfo"/>

  <el-drawer v-model="detailDrawer" size="30%">
    <my-list title="记录详情" :items="detailItems"/>
  </el-drawer>

</template>

<style scoped lang="scss">

</style>